package info.rongtong.action;

import info.rongtong.domain.Article;
import info.rongtong.service.inter.ArticleServiceInter;
import info.rongtong.service.inter.CategoryServiceInter;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class NoteManageAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private CategoryServiceInter categoryServiceImpl;
	private ArticleServiceInter articleServiceImpl;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private Article article;
	private Map<String, Object> map = new HashMap<String, Object>();
	private Integer pageSizes=5;
	private Integer pageCurrent = 0;
	private Integer cid = -1;
	
	public String goNoteManageUi(){
		//准备类别信息
		request.setAttribute("categoryList", categoryServiceImpl.getCategoryByType(0)) ;
		return "Ui";
	}
	
	//得到总页数
	public String getNoteManagePageCount(){
		if(cid==-1)
			map.put("pageCount", articleServiceImpl.getPageCount(pageSizes));
		else
			map.put("pageCount", articleServiceImpl.getPageCountByCategory(pageSizes,cid));
		return "json";
	}
	
	//开始查询数据
	public String getNoteManageList(){
		if(cid==-1){
			System.out.println("得到进入数据");
			map.put("List", articleServiceImpl.getArticleOrderBy(pageCurrent,pageSizes));
		}else{
			map.put("List", articleServiceImpl.getArticleOrderBy(pageCurrent,pageSizes,cid));
		}
		return "json";
	}
	//首页数据查询 
	public String getNoteManageIndexList(){
		if(cid==-1){
			map.put("List", articleServiceImpl.getArticleOrderBy(pageCurrent,pageSizes));
		}else{
			map.put("List", articleServiceImpl.getArticleOrderBy(pageCurrent,pageSizes,cid));
		}
		return "json";
	}
	
	public String addNoteUi(){
		//准备类别信息
		request.setAttribute("categoryList", categoryServiceImpl.getCategoryByType(0)) ;
		return "addNoteUi";
	}
	
	public String addNote(){
		if(article!=null){
			article.setAtime(new Date());
			articleServiceImpl.saveOrUpdate(article);
			//准备类别信息
			request.setAttribute("categoryList", categoryServiceImpl.getCategoryByType(0)) ;
			return "Ui";
		}
		return "Error";
	}
	
	public String delNote(){
		System.out.println(article.getAid());
		articleServiceImpl.delObjectById(Article.class, article.getAid());
		map.put("result", "ok");
		return "json";
	}
	
	public String goUpdateNoteUi(){
		//准备类别信息
		request.setAttribute("categoryList", categoryServiceImpl.getCategoryByType(0)) ;
		//准备要修改的数据
		request.setAttribute("article",  articleServiceImpl.findObjectById(Article.class, article.getAid()));
		return "updateNoteUi";
	}
	
	public String updateNote(){
		if(article!=null){
			article.setAtime(new Date());
			articleServiceImpl.saveOrUpdate(article);
			//准备类别信息
			request.setAttribute("categoryList", categoryServiceImpl.getCategoryByType(0)) ;
			return "Ui";
		}
		return "Error";
	}
	
	//得到某日志的详细信息
	public String getDetailNote(){
		//得到日志的详细信息和类别信息
		Object[] objects= (Object[]) articleServiceImpl.getArticleById(article.getAid());
		map.put("article", objects);
		//更新 点击次数
		Article articlee = (Article) objects[0];
		int temp = 1;
		if(articlee.getAreadNum()==null)
			articlee.setAreadNum(temp);
		else{
			temp = articlee.getAreadNum()+1;
			articlee.setAreadNum(temp);
		}
		articleServiceImpl.saveOrUpdate(articlee);
		//得到该日志的评论信息
		return "json";
	}
	
	//用户点击的推荐按钮的操作
	
	public String updateNoteArecommend(){
		Article articlee =  articleServiceImpl.findObjectById(Article.class, article.getAid());
		int temp = 1;
		if(articlee.getArecommend()==null)
			articlee.setArecommend(temp);
		else{
			temp = articlee.getArecommend()+1;
			articlee.setArecommend(temp);
		}
		articleServiceImpl.saveOrUpdate(articlee);
		System.out.println(temp);
		map.put("arecommend", temp);
		return "json";
	}
	
	
	//这里是set和get方法区
	public CategoryServiceInter getCategoryServiceImpl() {
		return categoryServiceImpl;
	}

	public void setCategoryServiceImpl(CategoryServiceInter categoryServiceImpl) {
		this.categoryServiceImpl = categoryServiceImpl;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public ArticleServiceInter getArticleServiceImpl() {
		return articleServiceImpl;
	}

	public void setArticleServiceImpl(ArticleServiceInter articleServiceImpl) {
		this.articleServiceImpl = articleServiceImpl;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public Integer getPageSize() {
		return pageSizes;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSizes = pageSize;
	}

	public Integer getPageCurrent() {
		return pageCurrent;
	}

	public void setPageCurrent(Integer pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	public Integer getCid() {
		return cid;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}
	
	
	
}
